<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\user_center\model\user;

use think\Model;

/**
 * 用户权限模型
 * @package app\user_center\model
 */
class UcMemberAuthtoken extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_uc_member_authtoken';

    /**
     * 生成用户权限信息
     * @param int $uid 用户id
     * @author jason wang <120907037@qq.com>
     * @return array
     */
    public function addData($uid, $create_time = ''){
        $authtoken_arr = create_authtoken($uid);//生成token
        $data['uid']                         = $uid;
        $data['access_token']                = $authtoken_arr['access_token'];
        $data['access_token_expire_in']      = $authtoken_arr['access_token_expire_in'];
        $data['access_token_expire_time']    = $authtoken_arr['access_token_expire_time'];
        $data['refresh_token']               = $authtoken_arr['refresh_token'];
        $data['refresh_token_expire_in']     = $authtoken_arr['refresh_token_expire_in'];
        $data['refresh_token_expire_time']   = $authtoken_arr['refresh_token_expire_time'];
		if($create_time != '')$data['create_time'] 	= $create_time;
        $res = $this -> isUpdate(false) ->data($data, true) -> save();
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 获取用户权限信息
     * @param int $uid 用户id
     * @author jason wang <120907037@qq.com>
     * @return array
     */
    public function getDataByUid($uid){
        $map = [];
        $map['uid'] = $uid;
        $info = self::where($map)->find();
        if(!empty($info)){
            return array_out(200, 'success', $info);
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 更新用户权限信息
     * @param int $uid 用户id
     * @author jason wang <120907037@qq.com>
     * @return array
     */
    public function updateDataByUid($uid){
        $authtoken_arr = create_authtoken($uid);//生成token
        $data = [];
        $data['access_token']                     = $authtoken_arr['access_token'];
        $data['access_token_expire_in']           = $authtoken_arr['access_token_expire_in'];
        $data['access_token_expire_time']         = $authtoken_arr['access_token_expire_time'];
        $data['refresh_token']                    = $authtoken_arr['refresh_token'];
        $data['refresh_token_expire_in']          = $authtoken_arr['refresh_token_expire_in'];
        $data['refresh_token_expire_time']        = $authtoken_arr['refresh_token_expire_time'];
        $map = [];
        $map['uid'] = $uid;
        $res = $this -> save($data, $map);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }
    /**
     * 退出登录清空TOKEN信息
     * @param int $uid 用户id
     * @author jason wang <120907037@qq.com>
     * @return array
     */
    public function WipeDataByUid($uid){
        $authtoken_arr = create_authtoken($uid);//生成token
        $data = [];
        $data['access_token']                     = '';
        $data['access_token_expire_in']           = '';
        $data['access_token_expire_time']         = '';
        $data['refresh_token']                    = '';
        $data['refresh_token_expire_in']          = '';
        $data['refresh_token_expire_time']        = '';
        $map = [];
        $map['uid'] = $uid;
        $res = $this -> save($data, $map);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }
    
}
